package com.kaluli.project.utils.nanshaxml;
import java.sql.Timestamp;
/**
 * 此类用于组装南沙国检局的推送报文
 * 此类的工作环境是多线程
 * @author zyzowen1944
 *  Date 2016-2-2
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.http.client.utils.DateUtils;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
public  class XmlBuilder {
	private static XmlBuilder xmlBuilder=null;
	private static Document document = null;
	private final static String ROOT="ROOT";
	private final static String HG_ROOT="Manifest";
	private final static String HEAD = "Head";
	//private final static String HG_HEAD = "Head";
	
	private final static String BODY = "Body";
	private final static String HG_BODY = "Declaration";
	public final static String MESSAGEID = "MessageID";
	public final static String MESSAGETYPE = "MessageType";
	public final static String SENDER = "Sender";
	public final static String HG_SENDER = "SenderID";
	public final static String RECEIVER = "Receiver";
	public final static String SENDTIME = "SendTime";
	public final static String FUNCTIONCODE = "FunctionCode";
	public final static String VERSION = "Version";
	
	public final static String TRADE_SWBEBTRADE = "swbebtrade";
	public final static String TRADE_RECORD = "Record";
	public final static String TRADE_ENTINSIDENO = "EntInsideNo";
	public final static String TRADE_CIQBCODE = "Ciqbcode";
	public final static String TRADE_CBECOMCODE = "CbeComcode";
	public final static String TRADE_CBEPCOMCODE = "CbepComcode";
	public final static String TRADE_ORDERSTATUS = "OrderStatus";
	public final static String TRADE_RECEIVENAME = "ReceiveName";
	public final static String TRADE_RECEIVEADDR = "ReceiveAddr";
	public final static String TRADE_RECEIVENO = "ReceiveNo";
	public final static String TRADE_RECEIVEPHONE = "ReceivePhone";
	public final static String TRADE_FCY = "FCY";
	public final static String TRADE_FCODE = "Fcode";
	public final static String TRADE_EDITCCODE = "Editccode";
	public final static String TRADE_DRDATE = "DrDate";
	
	public final static String TRADE_SWBEBTRADEG = "swbebtradeg";
	public final static String TRADE_ENTGOODSNO = "EntGoodsNo";
	public final static String TRADE_GCODE = "Gcode";
	public final static String TRADE_HSCODE = "Hscode";
	public final static String TRADE_CIQGOODSNO = "CiqGoodsNo";
	public final static String  TRADE_CopGName = "CopGName";
	public final static String TRADE_SPEC = "Spec";
	public final static String TRADE_ORIGIN = "Origin";
	public final static String TRADE_QTY = "Qty";
	public final static String TRADE_UPRIC = "Upric";
	public final static String TRADE_KGS = "Kgs";
	public final static String TRADE_NET = "Net";
	public final static String TRADE_QTYUNIT = "QtyUnit";
	public final static String TRADE_DECPRICE =  "DecPrice";
	public final static String TRADE_DECTOTAL = "DecTotal";
	public final static String TRADE_SELLWEBSITE = "SellWebSite";
	public final static String TRADE_NOTS = "Nots";
	
	public final static String HG_EORDER = "EOrder";
	public final static String HG_ORDERID = "OrderId";
	public final static String HG_IEFLAG = "IEFlag";
	public final static String HG_ORDERSTATUS="OrderStatus";
	public final static String HG_ENTRECORDNO = "EntRecordNo";
	public final static String HG_ENTRECORDNAME = "EntRecordName";
	public final static String HG_ORDERNAME = "OrderName";
	public final static String HG_ORDERDOCTYPE = "OrderDocType";
	public final static String HG_ORDERDOCID = "OrderDocId";
	public final static String HG_ORDERPHONE = "OrderPhone";
	public final static String HG_ORDERGOODTOTAL = "OrderGoodTotal";
	public final static String HG_ORDERGOODTOTALCURR = "OrderGoodTotalCurr";
	public final static String HG_FREIGHT = "Freight";
	public final static String HG_FREIGHTCURR = "FreightCurr";
	public final static String HG_TAX = "Tax";
	public final static String HG_TAXCURR = "TaxCurr";
	public final static String HG_NOTE = "Note";
	public final static String HG_ORDERDATE = "OrderDate";
	
	public final static String HG_EORDERGOODS = "EOrderGoods";
	public final static String HG_EORDERGOOD = "EOrderGood";
	public final static String HG_GNO = "GNo";
	public final static String HG_CHILDORDERNO = "ChildOrderNo";
	public final static String HG_STORERECORDNO = "StoreRecordNo";
	public final static String HG_STORERECORDNAME = "StoreRecordName";
	public final static String HG_CUSTOMSLISTNO = "CustomsListNO";
	public final static String HG_DECPRICE = "DecPrice";
	public final static String HG_UNIT = "Unit";
	public final static String HG_GQTY = "GQty";
	public final static String HG_DECLTOTAL = "DeclTotal";
	public final static String HG_NOTES = "Notes";
	
	
	
	
	
	
	public  static XmlBuilder getInstance(){
		synchronized(XmlBuilder.class){  //避免多线程调用该方法时串行执行，效率低下
			//if(xmlBuilder==null){
				xmlBuilder =  new XmlBuilder();
				
			//}
		}
		return xmlBuilder;
	}
	
	/**
	 * 创建海关Xml组装器
	 * @param root
	 * @param head
	 * @param body
	 * @return
	 */
	public static XmlBuilder getInstance(String type){
		synchronized(XmlBuilder.class){  //避免多线程调用该方法时串行执行，效率低下
			xmlBuilder = new XmlBuilder(type);
		}
		return xmlBuilder;
	}
	
	
	public XmlBuilder(){
		document = DocumentFactory.getInstance().createDocument();
		document.addElement(ROOT);
		Element root = document.getRootElement();
		root.addElement(HEAD);
		root.addElement(BODY);
	}
	
	public XmlBuilder(String root){
		document = DocumentFactory.getInstance().createDocument();
		document.addElement(HG_ROOT);
		Element roots = document.getRootElement();
		roots.addElement(HEAD);
		roots.addElement(HG_BODY);
	}
	
	public void addHeader(String key,String value){
		String xpath = "//"+HEAD;
		@SuppressWarnings("unchecked")
		List<Element> composites = document.selectNodes(xpath);
		Element head = composites.get(0);
		head.addElement(key).addText(value);
	}
	
	public void addBody(String key,Object value,boolean isleaf,String parentName){
		String xpath = "/"+parentName;
		@SuppressWarnings("unchecked")
		List<Element> composites = document.selectNodes(xpath);
		Element body = composites.get(composites.size()-1);
		if(isleaf){ //如果是叶子节点，同时创建Tag和Value
			if(value.getClass().getName().equals(String.class.getName())){
				body.addElement(key).addText(value.toString());
			}else if(value.getClass().getName().equals(Date.class.getName()) || value.getClass().getName().equals(Timestamp.class.getName())){
				String date = DateUtils.formatDate((Date) value, "yyyyMMddHHmmss");
				body.addElement(key).addText(date);
			}else{
				body.addElement(key).addText(value.toString());
			}
			
		}else{  //如果是子节点
			body.addElement(key);
		}
	}
	
	public String toString(){
		return document.asXML();
	}
	public static void main(String s[]){
		/*XmlBuilder xml = XmlBuilder.getInstance("HG");
		xml.addHeader("MessageID", "CUST000000012312313");+
		xml.addBody("swbebtrade", "", false, ROOT+"/"+BODY);
		xml.addBody("Record","",false,ROOT+"/"+BODY+"/swbebtrade");
		xml.addBody("EntInsideNo", "test0001", true, ROOT+"/"+BODY+"/swbebtrade/Record");//增加叶子 
		xml.addBody("swbetradeg",null, false	, ROOT+"/"+BODY+"/swbebtrade/Record");  //增加节点 
		xml.addBody("Record", "", false	, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg");  //增加节点 
		xml.addBody("EntGoodsNo", "1", true, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg/Record");
		xml.addBody("Record", "", false	, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg");  //增加节点 
		xml.addBody("EntGoodsNo", "2", true, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg/Record");
		//xml.addBody("", value, isleaf, parentName)
		
		//xml.addBody("EntGoodsNo", "1", true, parentName)
		System.out.println(xml.toString());
		SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
		String s2 = sFormat.format(new Date());
		System.out.print(s2);*/
	}
	
}
